<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class omnitrix_html_navigation extends omnitrix_html_abstract
{
	protected $_navigation = array();
	protected $_tree = array();

	protected function _fetch_navigation()
	{
		$event = otx_event_dispatch('admin_navmenu', array('admin_navmenu' => array()));
		$this->_merge_navigation($event->admin_navmenu);
	}

	protected function _merge_navigation($navagition, $parent = null)
	{
		foreach ($navagition as $name => $info) {
			if (null !== $parent) {
				$info['parent'] = $parent;
			}

			if (isset($info['children'])) {
				$children = (array) $info['children'];
			} else {
				$children = array();
			}

			unset($info['children']);
			$this->_navigation[$name] = $info;

			if ($children) {
				$this->_merge_navigation($children, $name);
			}
		}
	}

	protected function _build_tree()
	{
		$tree['__root__'] = array(
			'children' => array(),
			'before' => array(),
			'after' => array()
		);

		foreach (array_keys($this->_navigation) as $name) {
			$tree[$name] = array(
				'children' => array(),
				'before' => array(),
				'after' => array()
			);
		}

		foreach ($this->_navigation as $name => $info) {
			if (isset($info['before']) && isset($tree[$info['before']])) {
				$tree[$info['before']]['before'][] = $name;
			} else if (isset($info['after']) && isset($tree[$info['after']])) {
				$tree[$info['after']]['after'][] = $name;
			} else if (isset($info['parent']) && isset($tree[$info['parent']])) {
				$tree[$info['parent']]['children'][] = $name;
			} else if (empty($info['parent'])) {
				$tree['__root__']['children'][] = $name;
			}
		}

		$this->_tree = $tree;
	}

	protected function _render_tree()
	{
		$this->_build_tree();

		$html = otx_html_tag_start('div', array('id' => 'navmenu'));

		$html .= otx_html_tag_start('ul', array('class' => 'left sf-js-enabled'));
		foreach ($this->_tree['__root__']['children'] as $name) {
			if (empty($this->_navigation[$name]['float']) ||
				$this->_navigation[$name]['float'] == 'left') {
				$html .= $this->_render_node($name, 0);
			}
		}
		$html .= otx_html_tag_end('ul');

		$html .= otx_html_tag_start('ul', array('class' => 'right sf-js-enabled'));
		foreach ($this->_tree['__root__']['children'] as $name) {
			if (isset($this->_navigation[$name]['float']) &&
				$this->_navigation[$name]['float'] == 'right') {
				$html .= $this->_render_node($name, 0);
			}
		}
		$html .= otx_html_tag_end('ul');

		$html .= otx_html_tag_end('div');
		return $html;
	}

	public function _render_node($name, $level)
	{
		$html = '';
		$info = $this->_navigation[$name];

		foreach ($this->_tree[$name]['before'] as $before) {
			$html .= $this->_render_node($before, $level);
		}

		$html .= otx_html_tag_start('li');
		if ($this->_tree[$name]['children']) {
			$html .= otx_html_tag_simple('a', $info['title'], array('class' => $level == 0 ? 'top' : 'parent'));
			$html .= otx_html_tag_start('ul');
			foreach ($this->_tree[$name]['children'] as $child_name) {
				$html .= $this->_render_node($child_name, $level + 1);
			}
			$html .= otx_html_tag_end('ul');
		} else {
			$html .= otx_html_tag_simple('a', $info['title'], array('href' => empty($info['link']) ? '#' : $info['link'], 'class' => $level == 0 ? 'top' : 'leaf'));
		}
		$html .= otx_html_tag_end('li');

		foreach ($this->_tree[$name]['after'] as $after) {
			$html .= $this->_render_node($after, $level);
		}

		return $html;
	}

	public function render_html()
	{
		$this->_fetch_navigation();
		$html = $this->_render_tree();
		return $html;
	}
}